package com.youchuang.action.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.youchuang.action.PageAction;
import com.youchuang.bean.Comment;
import com.youchuang.common.Constant;
import com.youchuang.service.AdminService;

import net.sf.json.JSONObject;

/**
 * 管理员查看商品评论信息
 * 
 * @author Administrator
 *
 */
@Namespace(value = "/admin")
@ParentPackage("shopping-default")
public class CommentListAction extends PageAction {
	private JSONObject resultObj;
	private Logger logger = Logger.getLogger(CommentListAction.class.getSimpleName());
	
	@Resource
	private AdminService adminService;

	@Action(value = "commentList", results = {
			@Result(name = Constant.SUCCESS, type = "json", params = { "root", "resultObj" }) })
	public String execute() throws Exception {
		logger.info("excetion is execute");
		size = Constant.DEFAULT_SIZE;
		int totalPnum = adminService.findCommentCountByAll();

		if (totalPnum == 0) {
			maxPage = 1;
		} else if (totalPnum % size == 0) {
			maxPage = totalPnum / size;
		} else {
			maxPage = totalPnum / size + 1;
		}
		int begin = (page - 1) * size;

		List<Comment> limitComments = adminService.findCommentLimmit(begin, size);

		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(Constant.ROWS, limitComments);
		jsonMap.put(Constant.TOTAL, totalPnum);
		jsonMap.put(Constant.PAGE_SIZE, size);
		jsonMap.put(Constant.PAGE_NUMBER, page);
		resultObj = JSONObject.fromObject(jsonMap);
		logger.info("request json =" + resultObj.toString());
		return Constant.SUCCESS;
	}

	public JSONObject getResultObj() {
		return resultObj;
	}

	public void setResultObj(JSONObject resultObj) {
		this.resultObj = resultObj;
	}

}
